package com.qidian.server.workerThread;

import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import com.qidian.common.Globals;
import com.qidian.common.TableMeta;
import com.qidian.connection.util.ConnectionPool;


/**
 *  删除已回答正确的图片、空目录等，回收磁盘空间。
 *  在必要时创建目录。暂未使用
 * 
 * */
public final class DiskCleaner extends WorkerThread
{
	private static DiskCleaner diskCleaner = new DiskCleaner() ;
    private int interval = 30*1000 ;
    private int time_out_interval = 5*1000*60 ;// 5 minutes

    private DiskCleaner()
    {
    	
    }
    
    public static DiskCleaner getInstance()
    {
    	return diskCleaner ;
    }
    
    @Override
    public void init()
    {
        
    }
    
    public void run()
    {
        
        // Wait for message to come
        while(true)
        {
            if (Globals.getMessageBuffer().size() > 0)
                break ;
            
            try
            {
                sleep(interval) ;
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
        
        String path = Globals.getUploadDirAbsolutePath() ;
        String fileRelativePath = null ;

        File file = null ;
        Connection conn = null ;
        
        try
        {
            conn = ConnectionPool.getConnection() ;

            Statement stt = conn.createStatement() ;

            long max_msg_id = 0;
            String sql = null ;

            while(true)
            {
                // delete files at first
                max_msg_id = 0;
                sql = "select * from " + TableMeta.table_message +
                " where " + TableMeta.column_receive_time + " < " + (System.currentTimeMillis()-time_out_interval) + 
                " order by " + TableMeta.column_msg_id;
                ResultSet rs = stt.executeQuery(sql) ;
                while(rs.next())
                {
                    max_msg_id = rs.getLong(TableMeta.column_msg_id) ;
                    fileRelativePath = rs.getString(TableMeta.column_relative_path) ;
                    file = new File(path+fileRelativePath) ;
                    if (file.exists())
                        file.delete() ;
                }
                rs.close() ;

            
                // then delete records in table
                sql = " delete from " + TableMeta.table_message + 
                        " where " + TableMeta.column_receive_time + " < " + (System.currentTimeMillis() - time_out_interval) +
                        " and " + TableMeta.column_msg_id + "<=" + max_msg_id;
                stt.executeUpdate(sql) ;
                
                // sleep a while
                sleep(interval) ;
            }
            
            
        }
        catch (Exception e)
        {
            e.printStackTrace() ;
        }
        finally
        {
            ConnectionPool.freeConn(conn) ;
        }
        
    }

}
